﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

namespace DecisionCircle.Platform
{
    public class Triangle
    {
        internal static Polygon CreateTriangle(ref Polygon originTriangle, double dRadius, Color color)
        {
            Polygon triangle = new Polygon();
            triangle.Margin = originTriangle.Margin;
            triangle.Points = CreateACopyPointCollection(originTriangle.Points);
            triangle.RenderTransformOrigin = originTriangle.RenderTransformOrigin;
            triangle.RenderTransform = new CompositeTransform();
            (triangle.RenderTransform as CompositeTransform).Rotation = dRadius * (180 / Math.PI) +
                (originTriangle.RenderTransform as CompositeTransform).Rotation;
            triangle.Fill = new SolidColorBrush(color);

            return triangle;
        }

        private static PointCollection CreateACopyPointCollection(PointCollection pointCollection)
        {
            var points = new PointCollection();
            foreach (var point in pointCollection)
            {
                points.Add(new Point(point.X, point.Y));
            }

            return points;
        }

        internal static Polygon CreateTriangle(Point pLeftTop, double dRadius, int intRadius, Color color, ref Point pointCenter)
        {
            Point pRightBottom = GetPointRightBottom(pLeftTop, dRadius, intRadius);

            pointCenter = new Point((pRightBottom.X - pLeftTop.X) / 2 + pLeftTop.X, pRightBottom.Y);

            Polygon triangle = new Polygon();
            triangle.Margin = GetTriangleMargin(pLeftTop, pRightBottom);
            triangle.Points = GetPointsCollection(pLeftTop, pRightBottom);
            triangle.RenderTransformOrigin = new Point(0.5, 1);
            triangle.RenderTransform = new CompositeTransform();
            triangle.Fill = new SolidColorBrush(color);
            return triangle;            
        }

        private static PointCollection GetPointsCollection(Point pLeftTop, Point pRightBottom)
        {
            PointCollection points = new PointCollection();

            points.Add(new Point(0, 0));
            points.Add(new Point(pRightBottom.X - pLeftTop.X, 0));
            points.Add(new Point((pRightBottom.X - pLeftTop.X) / 2, pRightBottom.Y - pLeftTop.Y));

            return points;
        }

        private static Thickness GetTriangleMargin(Point pLeftTop, Point pRightBottom)
        {
            Thickness thickness;
            thickness = new Thickness(pLeftTop.X,
                pLeftTop.Y,
                Constant.PointWidthHeightParent.X -pRightBottom.X,
                Constant.PointWidthHeightParent.Y - pRightBottom.Y);

            return thickness;
        }

        private static Point GetPointRightBottom(Point pLeftTop, double dRadius, int intRadius)
        {
            double dx, dy, x, y;

            dx = 2 * Math.Sin(dRadius / 2) * (double)intRadius;
            dy = Math.Cos(dRadius / 2) * (double)intRadius;

            x = pLeftTop.X + dx;
            y = pLeftTop.Y + dy;

            return new Point(x, y);
        }
    }
}
